/* 
 * File:   log.cpp
 * Author: Faraj
 * 
 * Created on May 11, 2013, 9:23 PM
 */
#include "log.h"

#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO lib::common::log::_csbi;
#else
#endif    

string lib::common::log::_current_datetime() {
    time_t now = time(0);    
    char buf[80];    
    strftime(buf, sizeof (buf), "%Y-%m-%d %H:%M:%S", localtime(&now));
    return buf;
}

void lib::common::log::_msg(string type, string msg) {
    printf("%s [%s]\t%s\n", lib::common::log::_current_datetime().c_str(), type.c_str(), msg.c_str());
}

void lib::common::log::info(string msg) {
    _color("INFO");
    _msg("INFO", msg);
    _reset_color();
}

void lib::common::log::warn(string msg) {
    _color("WARN");
    _msg("WARN", msg);
    _reset_color();
}

void lib::common::log::error(string msg) {
    _color("ERROR");
    _msg("ERROR", msg);
    _reset_color();
}

void lib::common::log::trace(string msg) {
    _color("TRACE");
    _msg("TRACE", msg);
    _reset_color();
}

void lib::common::log::_color(string color) {
#ifdef _WIN32   
    HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hstdout, &log::_csbi);
    WORD wcolor;
    if (color == "ERROR") {
        wcolor = FOREGROUND_RED | FOREGROUND_INTENSITY;
    } else if (color == "WARN") {
        wcolor = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
    } else if (color == "TRACE") {
        wcolor = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
    } else if (color == "INFO") {
        wcolor = log::_csbi.wAttributes;
    }
    SetConsoleTextAttribute(hstdout, wcolor);
#else
#endif
}

void lib::common::log::_reset_color() {
#ifdef _WIN32          
    HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hstdout, log::_csbi.wAttributes);
#else
#endif
}